package com.example.homemodule.view.dialog;

import static android.view.Gravity.BOTTOM;


import static com.examination.mlib.constants.ClickApi.Home.click_rebuydrug;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.homemodule.model.bean.ReBuyEntity;
import com.example.homemodule.R;
import com.example.homemodule.adapter.DrugListAdapter;
import com.yilijk.base.basedialog.BaseDialog;
import com.yilijk.base.basedialog.DialogChain;
import com.yilijk.base.utils.ClickUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2022/01/04
 */
public class DrugDialog extends BaseDialog implements View.OnClickListener{

    private DrugListAdapter drugListAdapter;
    private TextView drugTitle;
    private TextView drugClose;
    private RecyclerView drugList;
    ArrayList<ReBuyEntity.DataBean> strings;
    public DrugDialog(@NonNull Context context) {
        super(context);
    }

    private DrugDialogCall drugDialogCall;

    public void setDrugDialogCall(DrugDialogCall drugDialogCall) {
        this.drugDialogCall = drugDialogCall;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_drug);


        drugTitle = (TextView) findViewById(R.id.drug_title);
        drugClose = (TextView) findViewById(R.id.drug_close);
        drugList = (RecyclerView) findViewById(R.id.drug_list);
        drugClose.setOnClickListener(this);

        strings = new ArrayList<>();
        drugListAdapter = new DrugListAdapter(strings);
        drugList.setLayoutManager(new LinearLayoutManager(getContext()));
        drugList.setAdapter(drugListAdapter);
        drugListAdapter.notifyDataSetChanged();
        ClickUtils.setAdapterItemChildFastOnClickListener(drugListAdapter, new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.rebuy_alert_fast) {
                    ClickUtils.sendEvent(click_rebuydrug,getContext(),"复诊购药药品预定点击");
                    if (!TextUtils.isEmpty(strings.get(position).getDoctorId())){
                        drugDialogCall.click(strings.get(position).getPrice()+"", strings.get(position).getDoctorId(),strings.get(position).getCommodityId(),strings.get(position).getCommodityType());
                    }else{
                        drugDialogCall.click(strings.get(position).getPrice()+"", strings.get(position).getCommodityId(),strings.get(position).getCommodityType());
                    }
                    dismiss();
                }
            }
        });

    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.drug_close){
            dismiss();
        }
    }

    @Override
    public void intercept(@NonNull DialogChain chain) {
        if (chain!=null){
            super.intercept(chain);
        }
    }

    @Override
    public void show() {super.show();
        Window window = getWindow();
        window.setGravity(BOTTOM);
        window.setWindowAnimations(R.style.BottomDialogStyle);
        WindowManager windowManager = window.getWindowManager();
        Display defaultDisplay = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = (int)(defaultDisplay.getWidth()); //设置宽度
        window.setAttributes(attributes);

    }

    public void setData(List<ReBuyEntity.DataBean> data){
        strings.clear();
        strings.addAll(data);
        drugListAdapter.notifyDataSetChanged();
        drugTitle.setText("共"+data.size()+"件商品");
        drugList.scrollToPosition(0);

    }

    public interface DrugDialogCall{
        void click(String price, String doctorId,String commodityId,String commodityType);
        void click(String price, String commodityId,String commodityType);
    }
}
